package com.company.cloud.tag.modules.app.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import com.company.cloud.tag.modules.app.controller.req.LabelGetByTypeParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.tag.modules.app.controller.req.LabelQuery4PageParam;
import com.company.cloud.tag.modules.base.entity.LabelInfo;
import com.company.cloud.tag.modules.base.service.LabelInfoService;
import com.company.cloud.tag.modules.cache.models.LabelModel;
import com.company.cloud.tag.modules.cache.utils.TagConvertUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/app/label/tag")
@Api(tags="App_Label_标签")
public class LabelController {


    @Autowired
    private LabelInfoService labelInfoService;

    @PostMapping("/query")
    @ApiOperation(value="查询",response = LabelModel.class)
    public BaseResult query(@RequestBody LabelQuery4PageParam param) {
        IPage<LabelInfo> page = labelInfoService.page(new Page<LabelInfo>(param.getPage(),param.getRows()),
                new QueryWrapper<LabelInfo>()
                        .eq(StringUtils.hasText(param.getLabelId()),"label_id",param.getLabelId())
                        .like(StringUtils.hasText(param.getLabelName()),"label_name",param.getLabelName())
                        .eq(StringUtils.hasText(param.getLabelTypeNo()),"label_type_no",param.getLabelTypeNo())
                        .eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
                        .orderByDesc("orders")
        );
        
        IPage<LabelModel> data = page.convert(new Function<LabelInfo, LabelModel>() {
            @Override
            public LabelModel apply(LabelInfo entity) {
                return TagConvertUtils.getLabel(entity);
            }
        });
        return BaseResult.ok(data);
    }

    @PostMapping("/getTagByTypes")
    @ApiOperation(value="根据分类获取标签信息",response = LabelModel.class)
    public BaseResult getTagByTypes(@RequestBody  LabelGetByTypeParam param){
        if(param==null || CollectionUtils.isEmpty(param.getTypeNos())){
            return BaseResult.ok();
        }
        List<LabelInfo> labelInfoList = labelInfoService.list(new QueryWrapper<LabelInfo>()
                .in("label_type_no",param.getTypeNos())
                .eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
                .orderByAsc("label_type_no","orders")
        );
        if(CollectionUtils.isEmpty(labelInfoList)){
            return BaseResult.ok();
        }
        Map<String,List<String>> data=new HashMap<>();

        labelInfoList.forEach(item->{
           List<String> tagList= data.get(item.getLabelTypeNo());
           if(tagList==null){
               tagList=new ArrayList<>();
               data.put(item.getLabelTypeNo(),tagList);
           }
           tagList.add(item.getLabelId());
        });
        return BaseResult.ok(data);
    }


    
    

}
